package com.kongjs.application.tenant;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.api.listener.MessageListener;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class TenantRoleHierarchyMessageListener implements MessageListener<String> , InitializingBean {
    @Resource
    private RedissonClient redissonClient;
    @Resource
    private TenantRoleHierarchyInitializer initializer;
    /*
    * 异步刷新
    * */
    @Override
    public void onMessage(CharSequence charSequence, String message) {
        log.info("channel {} message {}", charSequence, message);
        if (message.equals("all")) {
            initializer.initAllRoleHierarchy();
        } else {
            initializer.initRoleHierarchy(message);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        RTopic topic = redissonClient.getTopic("RoleHierarchy");
        topic.addListenerAsync(String.class, this);
    }
}
